let hookState = [];
let hookStateIndex = 0;
function scheduleUpdate() {}
function useState(initState) {
  hookState[hookStateIndex] = hookState[hookStateIndex] || initState;
  let currentIndex = hookStateIndex;
  function setState(newState) {
    // 不会合并老状态,直接覆盖老状态
    hookState[currentIndex] = newState;
    scheduleUpdate(); // 调度更新，hookStateIndex置为0，从头开始走useState
  }
  return [hookState[hookStateIndex++], setState()];
}

function useMemo(factory, deps) {
  if (hookState[hookStateIndex]) {
    // 有值，说明不是第一次是更新
    // 取出最近一次的值
    let [lastMemo, lastDeps] = hookState[hookStateIndex];
    // 判断是不是每一项都一样
    let same = deps.every((item, index) => item === lastDeps[index]);
    if (same) {
      hookStateIndex++;
      return lastMemo;
    } else {
      let newMemo = factory();
      hookState[hookStateIndex++] = [newMemo, deps];
      return newMemo;
    }
  } else {
    let newMemo = factory();
    hookState[hookStateIndex++] = [newMemo, deps];
    return newMemo;
  }
}

function useCallback(callback, deps) {
  if (hookState[hookStateIndex]) {
    // 有值，说明不是第一次是更新
    // 取出最近一次的值
    let [lastCallback, lastDeps] = hookState[hookStateIndex];
    // 判断是不是每一项都一样
    let same = deps.every((item, index) => item === lastDeps[index]);
    if (same) {
      hookStateIndex++;
      return lastCallback;
    } else {
      hookState[hookStateIndex] = [callback, deps];
      return callback;
    }
  } else {
    hookState[hookStateIndex++] = [callback, deps];
    return callback;
  }
}
